<template>
  <el-dialog
    v-dialog-drag
    title="导入JSON"
    :visible.sync="showImportJsonDialogFlag"
    :show-close="true"
    class="small-padding-dialog"
    center
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
  >
    <el-alert
      type="info"
      title="导入的JSON内容须符合下述格式，以保证顺利导入."
      show-icon
      class="alert-padding"
    />
    <code-editor
      id="ImportJson"
      v-model="importTemplate"
      :mode="'json'"
      :readonly="false"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doJsonImport"> 导入 </el-button>
      <el-button @click="showImportJsonDialogFlag = false"> 取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
/* eslint-disable vue/require-default-prop */
/* eslint-disable eol-last */
import CodeEditor from "Yan/components/code-editor/index";
export default {
  components: { CodeEditor },
  props: {
    designer: Object,
  },
  data() {
    return {
      showImportJsonDialogFlag: false,
      importTemplate: "",
    };
  },
  methods: {
    show() {
      this.importTemplate = JSON.stringify(
        this.designer.getImportTemplate(),
        null,
        "  "
      );
      this.showImportJsonDialogFlag = true;
    },
    doJsonImport() {
      try {
        const importObj = JSON.parse(this.importTemplate);
        // console.log(importObj);
        this.designer.loadFormJson(importObj);
        this.showImportJsonDialogFlag = false;
        this.$message.success("导入JSON成功");
        this.designer.emitHistoryChange();
        this.designer.emitEvent("form-json-imported", []);
      } catch (ex) {
        this.$message.error(ex + "");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .small-padding-dialog {
    .el-dialog__header {
      //padding-top: 3px;
      //padding-bottom: 3px;
      background: #f1f2f3;
    }

    .el-dialog__body {
      padding: 12px 15px 12px 15px;

      .el-alert.alert-padding {
        padding: 0 10px;
      }
    }

    .ace-container {
      border: 1px solid #dcdfe6;
    }
  }
}
</style>
